package com.sunfeng.n2;


import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * 异步与同步
 *
 * @author sunfeng
 * @date 2021/03/15 14:01
 **/
@Slf4j(topic = "c.Test02")
public class Test02 {
    public static void main(String[] args) throws Exception {
        sync2();

    }

    public  static  void sync2() throws Exception {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        Future<String> future = threadPool.submit(new CallableTask());
        Future<String> future2 = threadPool.submit(new CallableTask());


        log.info("main start");
        TimeUtils.sleep(2);
        try {
            // 这里会发生阻塞
            System.out.println(future.get());
            System.out.println(future2.get());
        } catch (Exception e) {

        } finally {
            threadPool.shutdown();
        }
        System.out.println("main end");


    }

    public  static  void sync() throws Exception {




        Callable<String> callable = () -> {
            log.info("进入异步任务");
            TimeUtils.sleep(2);
            log.info("异步任务执行完毕");
            return "测试异步";
        };

        log.info("主线程任务");
        String call = callable.call();
        log.info("返回的值为：{}",call);
    }
}
